<?php
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class UserrestController extends Uuch_RestController {
    protected $cdn = "http://cdn.uuch.com";

    public function indexAction() {
        
    }
    
    public function getAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $input = file_get_contents("php://input");
        $params = Zend_Json::decode($input);
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            $data = Zend_Json::encode ( $ifGoOn );
            echo $data;
            return;
        }
        if(!method_exists($this,$params["functionName"])){
            return array("Result"=>1006,"ResultMessage"=>"所需要的服务不存在或者暂不可用");
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        return;
    }
    
    public function postAction() {
        return false;
    }
    
    public function putAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        $input = file_get_contents("php://input");
        $params = Zend_Json::decode($input);
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            return Zend_Json::encode ( $ifGoOn );
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        echo Zend_Json::encode ( $returnData );
        return ;
    }
    
    public function deleteAction() {
        return false;
    }

    /** 
     * @return 用户登录
     */
    protected function UserLogin($params) {
        if(!array_key_exists("uuid", $params) || !array_key_exists("pwd", $params)) {
            return array("Result"=>1002,"ResultMessage"=>"传入参数错误-用户登录信息没有传入。");
        }
        if($params["uuid"] == "" || $params["pwd"] == "") {
            return array("Result"=>1,"ResultMessage"=>"密码或者帐号错误。");
        }
        $userItem = Application_Model_User::getByEmail($params["uuid"]);
        if(!$userItem){
            return array("Result"=>1,"ResultMessage"=>"密码或者帐号错误。");
        }
        if(strcmp($userItem->generatePassword($params["pwd"], $userItem->passwordsalt), $userItem->password) != 0){
            return array("Result"=>1,"ResultMessage"=>"密码或者帐号错误。");
        }
        $returnData = $this->prepareUser($userItem);
        $returnData["Result"] = 0;
        $returnData["ResultMessage"] = "登录成功";
        return $returnData;
    }
    /**
     * @return 准备用户信息
     */
    protected function prepareUser($userItem) {
        $returnData = array();
        $returnData["userInfo"]["auth"] = Uuch_Util::encryptString($userItem->Id."&".date("Y-m-d H:i:s"));
        $returnData["userInfo"]["uname"] = $userItem->Nikename;
        $returnData["userInfo"]["mphone"] = $userItem->Profile?$userItem->Profile->Tel:"暂无";
        $returnData["userInfo"]["gender"] = $userItem->Profile?($userItem->Profile->Sex):"未知";
        $returnData["userInfo"]["addr"] = $userItem->Profile?$userItem->Profile->address:"暂无";
        $returnData["userInfo"]["birth"] = $userItem->Profile?($userItem->Profile->Birthday=="0000-00-00"?"暂无":$userItem->Profile->Birthday):"暂无";
        $returnData["userInfo"]["email"] = $userItem->Email;
        $returnData["userInfo"]["Money"] = $userItem->Account->Money;
        $returnData["userInfo"]["exp"] = $userItem->Account->Score;
        $returnData["userInfo"]["vipg"] = $userItem->Account->UserGrade;
        $returnData["userInfo"]["vipgRmk"] = $userItem->Account->UserGradeName;
        $returnData["userInfo"]["signup"] = $userItem->Datecreated;
        return $returnData;
    }
    
    protected function UserModify($params) {
        $isAuth = $this->analysisAuth($params["head"]);
        if($isAuth["Result"] != 1) {
            return $isAuth;
        }
        $userItem = Application_Model_User::load($isAuth["userId"]);
        if(!$userItem) {
            return false;
        }
        $profileItem = $userItem->Profile;
        if($params["uname"] != ""){
            $userItem->Nikename = $params["uname"];
            $profileItem->NickName = $params["uname"];
        }
        if($params["email"] != ""){
            $userItem->Email = $params["email"];
        }
        $userItem->save();
        if($params["gender"] != ""){
            $profileItem->Sex = $params["gender"];
        }
        if($params["mphone"] != ""){
            $profileItem->Tel = $params["mphone"];
        }
        if($params["addr"] != ""){
            $profileItem->address = $params["addr"];
        }
        if($params["birth"] != ""){
            $profileItem->Birthday = $params["birth"];
        }
        $profileItem->save();
        $returnData = $this->prepareUser($userItem);
        $returnData["Result"] = 0;
        $returnData["ResultMessage"] = "修改成功";
        return $returnData;
    }
    
    
    protected function UserAccount($params) {
        $isAuth = $this->analysisAuth($params["head"]);
        if($isAuth["Result"] != 1) {
            return $isAuth;
        }
        $payFilter = new Application_Model_Filter_PayBillSearch();
        $payFilter->accountId = $isAuth["userId"];
        $payFilter->type = Application_Model_PayBill::TYPE_RECHANGE;
        $pageindex = $params["idx"]?($params["idx"]==1?0:($params["idx"]-1)*10):0;
        $payItem = Application_Model_PayBill::getFilter($payFilter, 10, $pageindex);
        if(!$payItem) {
            return false;
        }
        $returnData = $this->preparePayblll($payItem);
        $returnData["Result"] = 0;
        $returnData["ResultMessage"] = "获取成功";
        $returnData["auth"] = Uuch_Util::encryptString($isAuth["userId"]."&".date("Y-m-d H:i:s"));
        return $returnData;
    }
    
    protected function preparePayblll($payItem) {
        $returnData = array();
        $i = 0;
        foreach ($payItem["list"] as $payVal) {
            $returnData["plist"][$i]["id"] = $payVal->Id;
            $returnData["plist"][$i]["date"] = $payVal->dateCreated;
            $returnData["plist"][$i]["name"] = $payVal->payname;
            $returnData["plist"][$i]["amt"] = $payVal->money;
            $returnData["plist"][$i]["status"] = $payVal->statusname;
            $returnData["plist"][$i]["cashAmt"] = $payVal->userinfo->Account->Money;
            $i ++;
        }
        $returnData["count"] = $payItem["size"];
        $returnData["pages"] = $payItem["pages"];
        return $returnData;
    }
    
}